package com.hdc.lw.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SMSUtils {

    private static final Logger log = LoggerFactory.getLogger(SMSUtils.class);

    private static String ACCESSKEYID = "LTAI5tAfRoomSLEDK12Szxp5";
    private static String SECRET = "Bj7BCwB2qm7NznukOOVgkZKPCCc0ER";


    public static void sendMsg(String phone, String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESSKEYID, SECRET);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);  // 指定发送短信的请求方式
        request.setSysDomain("dysmsapi.aliyuncs.com");  // 标识使用的是阿里云的短信服务
        request.setSysVersion("2017-05-25");        // 服务的版本
        request.setSysAction("SendSms");        // 服务的API接口
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "毛信宇");
        request.putQueryParameter("TemplateCode", "SMS_109480109"); // 代表当前短信发送的内容
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("短信响应信息：" + response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
            throw new RuntimeException("阿里云服务器异常");
        } catch (ClientException e) {
            e.printStackTrace();
            throw new RuntimeException("短信发送异常");
        }
    }
}
